package com.ikas.ai.server.module.risk.controller;

import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.server.aop.Pageable;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.risk.model.RiskInspection;
import com.ikas.ai.server.module.risk.model.dto.RiskInspectionQueryDto;
import com.ikas.ai.server.module.risk.service.RiskInspectionService;
import com.ikas.ai.server.module.system.model.User;
import com.ikas.ai.utils.NumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 风险巡检项管理
 *
 * @author tang.xuandong
 * @version 1.0.0
 * @date 2023/1/31 11:54
 */
@RestController
@RequestMapping("/risk/inspection")
public class RiskInspectionController extends AbstractBean {

    @Autowired
    private RiskInspectionService riskInspectionService;

    /**
     * 获取风险巡检项分页数据
     *
     * @param req 风险巡检项分页DTO
     * @return JsonResult
     */
    @GetMapping("/page")
    @Pageable(sortType = "desc")
    public JsonResult pageQueryRiskInspection(RiskInspectionQueryDto req) {
        return JsonResult.ok(riskInspectionService.pageQueryRiskInspection(req));
    }

    /**
     * 获取风险巡检项，通过设备编码
     *
     * @param equipmentCode 设备编码
     * @return JsonResult
     */
    @GetMapping("/list/byEquipmentCode")
    public JsonResult getRiskInspectionByEquipmentCode(
            @RequestParam(value = "equipmentCode") String equipmentCode) {
        return JsonResult.ok(riskInspectionService.getRiskInspectionByEquipmentCode(equipmentCode));
    }


    /**
     * 新增风险巡检项信息
     *
     * @param param RiskInspection
     * @return JsonResult
     */
    @PostMapping("/add")
    public JsonResult addRiskInspection(@RequestBody @Validated RiskInspection param) {
        User user = getUser();
        param.setCreator(user.getId());
        return JsonResult.ok(riskInspectionService.saveRiskInspection(param));
    }

    /**
     * 修改风险巡检项信息
     *
     * @param param RiskInspection
     * @return JsonResult
     */
    @PostMapping("/update")
    public JsonResult updateRiskInspection(@RequestBody @Validated RiskInspection param) {
        User user = getUser();
        param.setCreator(user.getId());
        return JsonResult.ok(riskInspectionService.updateRiskInspection(param));
    }

    /**
     * 删除风险巡检项信息
     *
     * @param ids ids,多个用逗号隔开
     * @return JsonResult
     */
    @PostMapping("/{ids}")
    public JsonResult deleteRiskInspection(@PathVariable(name = "ids") String ids) {
        String[] idsStr = ids.split(",");
        List<Long> idsInt = new ArrayList<>();
        for (String id : idsStr) {
            if (!NumUtil.isInteger(id)) {
                continue;
            }
            idsInt.add(Long.parseLong(id));
        }
        return JsonResult.ok(riskInspectionService.deleteRiskInspection(idsInt));
    }

}
